package com.jlpay.openapi.demo.openmerch.register;

import com.jlpay.open.jlpay.sdk.java.model.openmerch.*;
import com.jlpay.open.jlpay.sdk.java.model.openmerch.register.*;
import com.jlpay.open.jlpay.sdk.java.model.openmerch.register.request.IncomingModifyRequest;
import com.jlpay.open.jlpay.sdk.java.model.openmerch.register.response.IncomingModifyResponse;
import com.jlpay.open.jlpay.sdk.java.utils.json.JsonUtils;
import com.jlpay.openapi.demo.JlpayOpenApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenjunhong
 * @since 2024/3/25
 */
public class IncomingModifyService {

    private static final Logger LOGGER = LoggerFactory.getLogger(IncomingModifyService.class);

    public static void main(String[] args) {
        IncomingModifyRequest request = buildRequest();
        IncomingModifyResponse response = JlpayOpenApiService.openMerchWithCryptoService()
                .post(request, IncomingModifyResponse.class);
        LOGGER.info("商户修改响应:{}", JsonUtils.toString(response));
    }

    private static IncomingModifyRequest buildRequest() {
        IncomingModifyRequest incomingAddRequest = new IncomingModifyRequest();
        // 基础信息
        incomingAddRequest.setBaseInfo(buildBaseInfo());
        // 身份信息
        incomingAddRequest.setIdentificationInfo(buildIdentificationInfo());
        // 经营信息
        incomingAddRequest.setBusinessInfo(buildBusinessInfo());
        // 结算信息
        incomingAddRequest.setSettlementInfo(buildSettlementInfo());
        // 结算规则
        incomingAddRequest.setSettlementRule(buildSettlementRule());
        // 补充信息
        incomingAddRequest.setAdditionalInfo(buildAdditionalInfo());

        return incomingAddRequest;
    }

    /**
     * 构建基础信息
     */
    private static BaseInfoDto buildBaseInfo() {
        // 基础信息
        BaseInfoDto baseInfoDto = new BaseInfoDto();
        baseInfoDto.setApplyId("ce0b904ec54d46ff8279b387ed4b9cca");
        baseInfoDto.setProductType(ProductType.QR_CODE_MERCHANT);
        baseInfoDto.setSignMode(SignMode.ONLINE_ELECTRONIC_AGREEMENT);
        baseInfoDto.setAgentId("50720711");
        baseInfoDto.setQrcodeTermAuto(BooleanEnum.TRUE);
        // 联系人信息
        ContactInfoDto contactInfoDto = new ContactInfoDto();
        contactInfoDto.setContactType(ContactType.OWNER_OR_LEGAL_PERSON);

        baseInfoDto.setContactInfo(contactInfoDto);
        return baseInfoDto;
    }

    /**
     * 构建身份信息
     */
    private static IdentificationInfoDto buildIdentificationInfo() {
        IdentificationInfoDto identificationInfo = new IdentificationInfoDto();
        identificationInfo.setMerchType(MerchType.BUSINESS_LICENSE);
        //营业证明信息
        identificationInfo.setLicenseInfo(buildLicenseInfo());
        //经营者/法人信息
        identificationInfo.setIdentityInfo(buildIdentityInfo());

        return identificationInfo;
    }

    /**
     * 构建经营信息
     */
    private static BusinessInfoDto buildBusinessInfo() {
        BusinessInfoDto businessInfo = new BusinessInfoDto();
        businessInfo.setMerchShortname("哈尔滨泰海新能源有限公司");
        businessInfo.setMccCode("5814");
        businessInfo.setAddressCode("440305");
        businessInfo.setAddressDetail("广东省深圳市南山区高新南");
        businessInfo.setDoorPic("region-ali-oss://jlpay-test/managefile/2024/02/28/1709081506952_af4f376e39d64abeb5dfb8a0c6db0da7.png");
        businessInfo.setCashierPic("region-ali-oss://jlpay-test/managefile/2024/02/28/1709081506952_af4f376e39d64abeb5dfb8a0c6db0da7.png");
        businessInfo.setIndoorPic("region-ali-oss://jlpay-test/managefile/2024/02/28/1709081506952_af4f376e39d64abeb5dfb8a0c6db0da7.png");
        return businessInfo;
    }

    /**
     * 构建结算信息
     */
    private static SettlementInfoDto buildSettlementInfo() {
        SettlementInfoDto settlementInfo = new SettlementInfoDto();
        settlementInfo.setSettleType(SettleType.TO_OWNER_BANK_CARD);
        settlementInfo.setSettleRelation(SettleRelation.SHAREHOLDER_OR_EMPLOYEE);
        // 账户信息
        settlementInfo.setAccountInfo(buildAccountInfo());
        // 授权收款材料
        settlementInfo.setOtherInfo(buildOtherInfo());

        return settlementInfo;
    }

    /**
     * 构建结算规则
     */
    private static SettlementRuleDto buildSettlementRule() {
        SettlementRuleDto settlementRule = new SettlementRuleDto();
        //结算设置
        settlementRule.setSettleInfo(buildSettleInfo());
        //费率设置
        settlementRule.setRateInfo(buildRateInfos());
        return settlementRule;
    }

    /**
     * 构建补充信息
     */
    private static AdditionalInfoDto buildAdditionalInfo() {
        AdditionalInfoDto additionalInfo = new AdditionalInfoDto();
        //渠道信息
        additionalInfo.setReportInfo(buildReportInfo());
        //辅助证明材料
        additionalInfo.setProofInfo(buildProofInfos());
        // 银行合作信息
        additionalInfo.setCoopBankInfo(buildCoopBankInfo());

        return additionalInfo;
    }

    /**
     * 构建渠道信息
     */
    private static ReportInfoDto buildReportInfo() {
        ReportInfoDto reportInfo = new ReportInfoDto();
        reportInfo.setWechatSubAppid("wx1234567890");
        return reportInfo;
    }

    /**
     * 构建辅助证明材料
     */
    private static List<ProofInfoDto> buildProofInfos() {
        List<ProofInfoDto> proofInfos = new ArrayList<>();
        proofInfos.add(buildProofInfo("assist_info_1", FileType.IMAGE, "region-ali-oss://jlpay-test/managefile/2024/02/28/1709106666879_6c53af86b9654680a727b92795dce6c2.jpg"));
        return proofInfos;
    }

    /**
     * 构建营业证明信息
     */
    private static LicenseInfoDto buildLicenseInfo() {
        // 营业证明信息
        LicenseInfoDto licenseInfo = new LicenseInfoDto();
        licenseInfo.setLicenseType(LicenseType.COMPANY);
        licenseInfo.setLicensePic("region-ali-oss://jlpay-test/managefile/2024/02/28/1709081506952_af4f376e39d64abeb5dfb8a0c6db0da7.png");
        licenseInfo.setLicenseName("哈尔滨泰海新能源有限公司");
        licenseInfo.setLicenseNo("91330108MABTT5NP6U");
        licenseInfo.setBusinessScope("货币");
        return licenseInfo;
    }

    /**
     * 构建经营者/法人信息
     */
    private static IdentityInfoDto buildIdentityInfo() {
        // 经营者/法人信息
        IdentityInfoDto identityInfo = new IdentityInfoDto();
        identityInfo.setIdDocType(IdType.ID_CARD);
        identityInfo.setIdCardName("王一");
        identityInfo.setIdCardNo("替换为身份证");
        identityInfo.setLegalPersonPhone("替换为手机号");
        identityInfo.setCardPeriodBegin("2024-01-01");
        identityInfo.setCardPeriodEnd("2030-01-01");
        identityInfo.setIdCardAddress("哈尔滨泰海新能源有限公司");
        identityInfo.setIdCardCopy("region-ali-oss://jlpay-test/managefile/2024/02/28/1709081474283_7ddfb60f42d94f8094ec88d7c78ec4a3.png");
        identityInfo.setIdCardNational("region-ali-oss://jlpay-test/managefile/2024/02/28/1709081506952_af4f376e39d64abeb5dfb8a0c6db0da7.png");

        return identityInfo;
    }

    /**
     * 构建结算账户信息
     */
    private static AccountInfoDto buildAccountInfo() {
        AccountInfoDto accountInfo = new AccountInfoDto();
        accountInfo.setAccountNo("替换为银行卡");
        accountInfo.setAccountName("王一");
        accountInfo.setCertType("01");
        accountInfo.setBankCode("082");
        accountInfo.setBankBranchId("105584000370");
        accountInfo.setBankBranchName("中国建设银行股份有限公司深圳罗湖支行");
        accountInfo.setAccountPic("region-ali-oss://jlpay-test/managefile/2024/02/28/1709106666879_6c53af86b9654680a727b92795dce6c2.jpg");

        return accountInfo;
    }

    /**
     * 构建授权收款材料
     */
    private static OtherInfoDto buildOtherInfo() {
        OtherInfoDto otherInfo = new OtherInfoDto();
        otherInfo.setIdentityFacePic("region-ali-oss://jlpay-test/managefile/2024/02/28/1709106666879_6c53af86b9654680a727b92795dce6c2.jpg");
        return otherInfo;
    }

    /**
     * 构建结算设置
     */
    private static SettleInfoDto buildSettleInfo() {
        SettleInfoDto settleInfo = new SettleInfoDto();
        settleInfo.setSettleMode(SettleMode.AUTO_T1);
        return settleInfo;
    }

    /**
     * 构建费率信息
     *
     * @return 费率列表
     */
    private static List<RateInfoDto> buildRateInfos() {
        //费率设置
        List<RateInfoDto> rateInfo = new ArrayList<>();
        // 微信费率
        rateInfo.add(buildRateInfo(FeeCalcType.WECHAT_PAY,
                "0", "-1",
                "0", "0.3",
                "0", "-1",
                "0", "0", "0"));
        // 支付宝费率
        rateInfo.add(buildRateInfo(FeeCalcType.ALIPAY,
                "0", "-1",
                "0", "0.3",
                "0", "-1",
                "0", "0", "0"));
        // 银联二维码费率
        rateInfo.add(buildRateInfo(FeeCalcType.UNIONPAY_QRCODE,
                "0", "-1",
                "0", "0.3",
                "0", "-1",
                "0", "0", "0"));
        // 银联内卡借记卡
        rateInfo.add(buildRateInfo(FeeCalcType.DOMESTIC_DEBIT_CARD,
                "0", "-1",
                "0", "0.3",
                "0", "-1",
                "0", "0", "0"));

        // 银联内卡贷记卡
        rateInfo.add(buildRateInfo(FeeCalcType.DOMESTIC_CREDIT_CARD,
                "0", "-1",
                "0", "0.3",
                "0", "-1",
                "0", "0", "0"));
        // 外卡借记卡费率
        rateInfo.add(buildRateInfo(FeeCalcType.INTERNATIONAL_DEBIT_CARD,
                "0", "-1",
                "0", "0.45",
                "0", "1900",
                "0", "0", "0"));
        // 外卡贷记卡费率
        rateInfo.add(buildRateInfo(FeeCalcType.INTERNATIONAL_CREDIT_CARD,
                "0", "-1",
                "0", "0.55",
                "0", "-1",
                "0", "0", "0"));

        return rateInfo;
    }

    /**
     * 构建费率信息
     *
     * @param feeCalcType 计费类型
     * @param minAmt      起始金额
     * @param maxAmt      截止金额
     * @param fixed       费率标识
     * @param rate        费率
     * @param bottom      保底收费
     * @param top         封顶收费
     * @param step        阶梯收费
     * @param baseAmount  阶梯基础金额
     * @param baseFee     阶梯基础金额以下费用
     * @return 费率信息
     */
    private static RateInfoDto buildRateInfo(FeeCalcType feeCalcType,
                                             String minAmt, String maxAmt,
                                             String fixed, String rate,
                                             String bottom, String top,
                                             String step, String baseAmount, String baseFee) {
        RateInfoDto rateInfo = new RateInfoDto();
        rateInfo.setFeeCalcType(feeCalcType);
        rateInfo.setMinAmt(minAmt);
        rateInfo.setMaxAmt(maxAmt);
        rateInfo.setFixed(fixed);
        rateInfo.setRate(rate);
        rateInfo.setBottom(bottom);
        rateInfo.setTop(top);
        rateInfo.setStep(step);
        rateInfo.setBaseAmount(baseAmount);
        rateInfo.setBaseFee(baseFee);
        return rateInfo;
    }

    /**
     * 银行合作信息
     */
    private static CoopBankInfoDto buildCoopBankInfo() {
        CoopBankInfoDto coopBankInfo = new CoopBankInfoDto();
        coopBankInfo.setBankServerId("10121452");
        coopBankInfo.setBankCode("10121452");
        return coopBankInfo;
    }

    /**
     * 辅助证明材料
     *
     * @param type      附件类型
     * @param fileType  文件类型
     * @param mediaPath 视频地址
     * @return
     */
    private static ProofInfoDto buildProofInfo(String type, FileType fileType, String mediaPath) {
        ProofInfoDto proofInfoDto = new ProofInfoDto();
        proofInfoDto.setType(type);
        proofInfoDto.setFileType(fileType);
        proofInfoDto.setMediaPath(mediaPath);
        return proofInfoDto;
    }

}
